<style type="text/css">
.normal_form[form-role="dialog"] {padding-top:30px;}
.normal_form[form-role="dialog"] .input-text{width:250px;}
</style>
<form name="dialogform" class="normal_form" form-role="dialog" onsubmit="return Core.Sys.Dialogform(this);" autocomplete="off">
    <div class="row">
        <div class="field_text">角色：</div>        
        <select class="z-select groupid-select" name="groupid" data-style="width:250px;">
            <option value="">请选择角色</option>
            <!--{if isset($group) AND $group}-->
            {foreach $group as $key => $row}
            <option value="{$row['id'] ?? ''}">{$row['title'] ?? ''}</option>
            {/foreach}
            <!--{/if}-->
        </select>        
    </div>
    <div class="row">
        <div class="field_text">手机号：</div>
        <input type="text" class="input-text" name="mobile" maxlength="11" placeholder="请输入手机号" style="width:250px;" />
    </div>
    <div style="color:gray;text-indent:105px;line-height:25px;"></div>
    <div class="row">
        <div class="field_text">姓名：</div>
        <input type="text" class="input-text" name="realname" maxlength="30" placeholder="请输入姓名" style="width:250px;" />
    </div>
    <div class="row">
        <div class="field_text">性别：</div>
        <div class="labelrow" >
            <label class="l-radio"><input type="radio" name="gender" value="1" {if ($res['gender'] ?? 1)==1}checked{/if} /><i></i><span>男</span></label>
            <label class="l-radio"><input type="radio" name="gender" value="2" {if ($res['gender'] ?? 0)==2}checked{/if} /><i></i><span>女</span></label>
        </div>
    </div>
    <div class="row form-footer-bar">
        <p class="colortip"></p>
        <button class="btn" role="btn-cancel">取消</button>
        <button class="btn btn-primary" type="submit" role="btn-submit">保存</button>
    </div>
</form>
<script type="text/javascript">
var isuser = false;//是否已有账户 true - 是,false - 否
Core.Sys.Dialogform = function(form){
    var form = $(form);
    var p    = {
        'groupid'  : $(".groupid-select").getZsValue(),
        'mobile'   : form.find(":text[name='mobile']").val(),
        'realname' : form.find(":text[name='realname']").val(),
        'gender'   : form.find(":radio:checked[name='gender']").val() || 0,
	}

    if (!Core.rule('intp',p['groupid'])){
		form.find("p.colortip").html('请选择角色!');return false;
	} else if (!Core.rule('mobile',p['mobile'])){
		form.find("p.colortip").html('请输入手机号!');return false;
	}
    if (!isuser && !Core.rule('*',p['realname'])) {
        form.find("p.colortip").html('请输入姓名!');return false;
    }

	form.find(":button").attr('disabled','disabled');
	$.ajax({type : "POST",dataType: "json",cache : false, url : "{$_G['_mca_']}?r="+Math.random(),data : p,success: function(data){
	   try{
		   if (data['state'] == 'y'){
                $('#datalist').datagrid('reload');$(form).dialogClose();
		   } else {
                form.find(":button").removeAttr('disabled');form.find("p.colortip").html(data['msg']);return false;
		   }
	   }catch(e){form.find(":button").removeAttr('disabled');form.find("p.colortip").html('数据提交失败!');return false;}
	}});
	return false;
}
Core.Sys.Mobile = function(mobile){
    var form   = $('form[name="dialogform"]');
    form.find(':text[name="realname"]').val('');
    form.find(':text[name="realname"]').removeAttr('disabled'); 
    form.find(":radio[name='gender']").removeAttr('disabled');
    if (!Core.rule('mobile',mobile)) {
        Core.Dialog.msg("请输入正确的手机号");return false;
    }
    isuser = false;
    $.getJSON("{$_G['_mc_'] ?? ''}/repeat/mobile/"+mobile+"?r="+Math.random(),function(json){
        if (json.state != 'y') {
            Core.Dialog.msg(json.msg || '接口数据错误');return false;
        } else {
            if (json.data['isuser'] || '') {
                isuser = true;
            }
            form.find(':text[name="realname"]').val(json.data['realname'] || '');
            isuser && form.find(':text[name="realname"]').attr('disabled','disabled');//默认
            if (json.data['gender'] || '') {
                //form.find(":radio:checked[name='gender']").prop('checked',false);
                form.find(":radio[name='gender'][value='"+(json.data['gender'])+"']").prop('checked',true);
                form.find(":radio[name='gender']").attr('disabled','disabled');
            }
        }
    });
}
$(function(){
    $(':text[name="mobile"]').change(function(){
        Core.Sys.Mobile($(this).val());
    });
});
</script>